#!/usr/bin/env python
#coding:utf-8

import tornado
from ..baseHandler import BaseHandler
from bson import ObjectId
from ..error import ErrorHandler
import uuid

class BannerHandler(BaseHandler):
    #获取app主页banner列表接口,考虑采取缓存技术
    @tornado.gen.coroutine
    def get(self):
        lists = yield self.db.banner.find({}).to_list(10)
        print(lists)
        if lists:
            return self.response_msg(200,'ok',self.find_formart(lists))
        else:
            return ErrorHandler.write_error(self,status_code=404,message='没有找到相关信息')

    @tornado.gen.coroutine
    def post(self, *args, **kwargs):
        (arguments,error) = yield self.get_select_argument(['image','type','sort'])
        if not arguments:
            return ErrorHandler.write_error(self,status_code=101,message=error)
        #插入数据
        res = yield self.db.banner.insert(arguments)
        if res:
            return self.response_msg(200,"插入成功")
        else:
            return ErrorHandler.write_error(self,status_code=102,message="插入失败")

    @tornado.gen.coroutine
    def put(self, *args, **kwargs):
        (arguments,error) = yield self.get_select_argument(['image','type','sort'])
        if not arguments:
            return ErrorHandler.write_error(self,status_code=101,message=error)
        #插入数据
        res = yield self.db.banner.update(arguments)
        if res:
            return self.response_msg(200,"插入成功")
        else:
            return ErrorHandler.write_error(self,status_code=102,message="插入失败")


class TopListHandler(BannerHandler):
    def get(self):
        pass


handlers = [(r"/banner",BannerHandler),
            ]